package com.rongshu.view.login.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.LoginUtils;
import com.rongshu.view.base.AdapterListener;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.adapter.SelectLabelAdapter;
import com.rongshu.view.home.bean.LabelResponse;
import com.rongshu.view.home.contract.PerfectLableView;
import com.rongshu.view.home.presenter.PerfectLablePresenter;
import com.utils.FastClickUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static com.rongshu.utils.UserInfoUtils.getUserId;

public class PerfectLableActivity extends BaseActivity<PerfectLablePresenter> implements PerfectLableView {
    @BindView(R.id.labels)
    RecyclerView labels;
    @BindView(R.id.btn_sure)
    Button btn_sure;

    //map
    private Map<String, Object> map = null;
    //adapter
    private SelectLabelAdapter mSelectLabelAdapter = null;
    //manager
    private GridLayoutManager manager = null;
    //list
    private List<LabelResponse.ResponseBean> tagsList = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_perfect_lable_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new PerfectLablePresenter(this), this);
        //manager
        manager = new GridLayoutManager(this, 3);
        //list
        tagsList = new ArrayList<>();
        //adapter
        mSelectLabelAdapter = new SelectLabelAdapter(this, tagsList);
        //recycler
        labels.setLayoutManager(manager);
        labels.setAdapter(mSelectLabelAdapter);
        //listener
        mSelectLabelAdapter.setAdapterListener(new AdapterListener() {
            @Override
            public void callBack(Object object) {
                for (LabelResponse.ResponseBean bean : tagsList) {
                    if (bean.getIsSelected()) {
                        btn_sure.setBackgroundResource(R.drawable.rectangle_three_green);
                        btn_sure.setEnabled(true);
                        return;
                    }
                    btn_sure.setBackgroundResource(R.drawable.rectangle_three_white);
                    btn_sure.setEnabled(false);
                }
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getlabels();
    }

    /**
     * 获取标签
     */
    private void getlabels() {
        map = new HashMap<>();
        map.put("rnd", 0);
        map.put("type", 3);//标签类型(1.个人分类 2.笔友会分类 3.文章分类)
        presenter.getlabels(0, 3, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 完善标签
     */
    private void perfectLable(List<Integer> integers) {
        int user_id = getUserId();
        if (user_id == 0) {
            LoginUtils.toLogin(this);
            return;
        }
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.postPerfectLable(user_id, Common.encryptionSign(map), integers);
        map.clear();
    }

    @OnClick({R.id.btn_cancel, R.id.btn_sure})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel://跳过
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                finish();
                break;
            case R.id.btn_sure://确定
                if (FastClickUtils.isFastClick()) {
                    return;
                }
                List<LabelResponse.ResponseBean> list = getSelectedLabel();
                if (list == null || list.isEmpty()) {
                    return;
                }
                //获取标签id
                List<Integer> integers = new ArrayList<>();
                for (LabelResponse.ResponseBean bean : list) {
                    integers.add(bean.getId());
                }
                //完善标签
                perfectLable(integers);
                break;
        }
    }

    /**
     * 选择选择中的标签
     *
     * @return
     */
    private List<LabelResponse.ResponseBean> getSelectedLabel() {
        List<LabelResponse.ResponseBean> selectedList = new ArrayList<>();
        for (LabelResponse.ResponseBean bean : tagsList) {
            if (bean.getIsSelected()) {
                selectedList.add(bean);
            }
        }
        return selectedList;
    }

    /**
     * 标签回调
     *
     * @param response
     */
    @Override
    public void onLabels(LabelResponse response) {
        if (response.getErrCode() == 0) {
            List<LabelResponse.ResponseBean> responseBeanList = response.getResponse();
            if (responseBeanList != null) {
                this.tagsList.addAll(responseBeanList);
                mSelectLabelAdapter.notifyDataSetChanged();
            }
        } else {
            toast(response.getErrMsg());
        }
    }

    /**
     * 完善标签回调
     *
     * @param bean
     */
    @Override
    public void onPerfectLables(INetBean bean) {
        if (bean.getErrCode() == 0) {
            finish();
        } else {
            toast(bean.getErrMsg());
        }
    }
}
